package com.shop.shoporder.wapController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.shoporder.common.*;

import com.shop.shoporder.common.UserLoginToken;
import com.shop.shoporder.entity.*;
import com.shop.shoporder.service.GoodsService;
import com.shop.shoporder.service.SpecService;
import com.shop.shoporder.service.StoreGoodsService;
import com.shop.shoporder.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.*;


@CrossOrigin
@Controller
@RequestMapping("/wap")
public class StoreController {

    @Autowired
    StoreService storeService;
    @Autowired
    StoreGoodsService storeGoodsService;
    @Autowired
    GoodsService goodsService;
    @Autowired
    SpecService specService;

    @Resource
    HttpRequestConfig requestConfig;


    /**
     * 进入店铺
     *
     * @return
     */

    @UserLoginToken
    @ResponseBody
    @RequestMapping("/enterStore")
    public Response enterStore(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        int userNoId = 0;
//            int storeId = 0;
        String userNo = null;
        int rankId = 0;
        String userNoIds = request.getParameter("userNoId");
        if (StringUtil.isNumeric(userNoIds)) {
            userNoId = Integer.parseInt(userNoIds);
        }


        //查询店铺信息
        Store store = storeService.selectStoreInfo(userNoId);
        map.put("store", store);
        if (store == null) {
            return new Response(MessageConstants.MSG_SUCCESS, map, 0, "", request);
        }

        String url = HttpRequestConfig.userServerUrl + "/wap/getUserNoInfo";
        String storeUserId = "userNoId=" + userNoId;
        String result = HttpRequest.sendGet(url, storeUserId);
        System.out.println("------>" + result);
        JSONObject jsonobject = JSONObject.parseObject(result);
        String code = jsonobject.get("code").toString();
        if ("000000".equalsIgnoreCase(code)) {
            String data = jsonobject.get("data").toString();
            JSONObject userNoData = JSONObject.parseObject(data);
            userNo = (String) userNoData.get("no");
            rankId = (int) userNoData.get("rankId");
        }

        map.put("userNo", userNo);
        map.put("rankId", rankId);

        return new Response(MessageConstants.MSG_SUCCESS, map, 0, "", request);

    }

    /**
     * 店铺商品管理
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/getStoreGoodsInfo")
    public Response getStoreGoodsInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        String pages = request.getParameter("page");
        int page = 0;
        int limit = 5;
        int userNoId = 0;
        if (token != null) {
            userNoId = TokenUtils.getUserNoId(token);
            if (userNoId != 0) {
                if (pages != null && pages != "") {
                    if (StringUtil.isNumeric(pages)) {
                        page = Integer.parseInt(pages);
                    }
                }
                //查询店铺的商品
                int storeId = 0;
                Store store = storeService.selectStoreInfo(userNoId);
                if (store == null) {
                    return new Response(MessageConstants.NO_APPLY_STORE, null, 0, "", request);
                }
                storeId = store.getStoreId();
//                System.out.println("storeId="+storeId);
//                String url = "http://192.168.0.133:8086/wap/getUserNoInfo";
//                String storeUserId = "userNoId=" + userNoId;
//                String result = HttpRequest.sendGet(url, storeUserId);
//                System.out.println("------>"+result);
//                JSONObject jsonobject = JSONObject.parseObject(result);
//                String code=jsonobject.get("code").toString();
                PageHelper.startPage(page, limit);
                List<WapStoreGoods> storeGoods = storeGoodsService.getStoreGoods(storeId);
                PageInfo<WapStoreGoods> pageInfo = new PageInfo<>(storeGoods);
                return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
            } else {
                return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
            }

        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }

    }

    /**
     * 获取店铺上架的商品列表
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/getStoreGoodsList")
    public Response getStoreGoodsList(HttpServletRequest request) {
        String token = request.getHeader("token");
        String pages = request.getParameter("page");
        if (token == null || token == "" || !StringUtil.isNumeric(pages)) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        int userNoId = 0;
        int page = 0;
        int limit = 5;
        userNoId = TokenUtils.getUserNoId(token);
        page = Integer.parseInt(pages);
        //查询店铺的商品
        int storeId = 0;
        Store store = storeService.selectStoreInfo(userNoId);
        if (store == null) {
            return new Response(MessageConstants.NO_APPLY_STORE, null, 0, "", request);
        }
        storeId = store.getStoreId();
        PageHelper.startPage(page, limit);
        List<WapStoreGoods> storeGoods = storeGoodsService.getStoreGoodsList(storeId);
        PageInfo<WapStoreGoods> pageInfo = new PageInfo<>(storeGoods);
        return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
    }


    /**
     * 申请店铺时增加店铺
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/applyStoreInfo")
    public Response applyStoreInfo(HttpServletRequest request) {
        String userNoIds = request.getParameter("userNoId");
        String stroeName = request.getParameter("storeName");
        String storeLogo = request.getParameter("storeLogo");

        System.out.println("----" + userNoIds + "-----" + stroeName);
        int userNoId = 0;

        if (userNoIds != null || userNoIds != "") {
            userNoId = Integer.parseInt(userNoIds);
        }

        Store store = storeService.selectStoreInfo(userNoId);
        if (store == null) {//可以申请
            Store store1 = new Store();
            store1.setApplyTime(new Timestamp(System.currentTimeMillis()));
            store1.setStoreName(stroeName);
            store1.setUserNoId(userNoId);
            store1.setStoreLogo(storeLogo);
            int row = storeService.insertSelective(store1);
            if (row > 0) {
                return new Response(MessageConstants.MSG_SUCCESS, true, 0, "", request);
            } else {
                return new Response(MessageConstants.MSG_OPERATE_FAILED, false, 0, "", request);
            }
        } else {
            return new Response(MessageConstants.STORE_EXIT, false, 0, "", request);
        }
    }


//    /**
//     * 店铺设置
//     *
//     * @param request
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping("/setStoreInfo")
//    public Response setStoreInfo(HttpServletRequest request) {
//        //查询店铺信息
//        String token = request.getHeader("token");
//        if (token == null) {
//            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
//        }
//        int userNoId = 0;
//        userNoId = TokenUtils.getUserNoId(token);
//        if (userNoId == 0) {//用户不存在
//            return new Response(MessageConstants.MSG_USER_INEXISTENCE, null, 0, "", request);
//        }
//
//        Store store = storeService.selectStoreInfo(userNoId);
//
//        return new Response(MessageConstants.MSG_SUCCESS, store, 0, "", request);
//    }


    /**
     * 设置店铺的名称、修改
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/setStoreName")
    public Response setStoreName(HttpServletRequest request) {
        //查询店铺信息
        String storeName = request.getParameter("storeName");
        String storeLogo = request.getParameter("storeLogo");
        String storeIds = request.getParameter("storeId");
        String token = request.getHeader("token");
        if (token == null) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        int userNoId = 0;
        userNoId = TokenUtils.getUserNoId(token);
        if (userNoId == 0) {//用户不存在
            return new Response(MessageConstants.MSG_USER_INEXISTENCE, null, 0, "", request);
        }
        if (storeIds == null || storeIds == "" || StringUtil.isNumeric(storeIds)) {//用户不存在
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        int storeId = Integer.parseInt(storeIds);
        Store store = new Store();
        store.setStoreName(storeName);
        store.setStoreLogo(storeLogo);
        store.setStoreId(storeId);
        int row = storeService.updateByPrimaryKeySelective(store);
        if (row > 0) {
            return new Response(MessageConstants.MSG_SUCCESS, store, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
        }

    }


    /**
     * 查询关注店铺列表
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/concernStoreList")
    public Response concernStoreList(HttpServletRequest request) {
        String token = request.getHeader("token");
        String pages = request.getParameter("page");

        int page = 0;
        if (pages != null && pages != "") {
            page = Integer.parseInt(pages);
        }
        int limit = 5;
        if (token == null) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        int userNoId = 0;
        userNoId = TokenUtils.getUserNoId(token);
        if (userNoId == 0) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }

        List list = new ArrayList();

        String url = HttpRequestConfig.userServerUrl + "/wap/getConernStoreId";
        String UserNoId = "userNoId=" + userNoId;
        String result = HttpRequest.sendGet(url, UserNoId);
        JSONObject jsonobject = JSONObject.parseObject(result);
        String code = jsonobject.get("code").toString();

        if ("000000".equalsIgnoreCase(code)) {
            Object data = jsonobject.get("data");
            JSONArray dataArr = JSONArray.parseArray(data.toString());
            System.out.println("data=" + dataArr);
            for (int i = 0; i < dataArr.size(); i++) {
                JSONObject obj = dataArr.getJSONObject(i);
                int storeId = Integer.parseInt(obj.get("storeId").toString());
                list.add(storeId);

            }


        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        List<Store> storeList=new ArrayList<>();
        if(!list.isEmpty()&&list.size()!=0){
            PageHelper.startPage(page, limit);
           storeList  = storeService.getStoreList(list);
            PageInfo<Store> pageInfo = new PageInfo<>(storeList);

            return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
        }else{
            return new Response(MessageConstants.MSG_SUCCESS, storeList,0, "", request);
        }

    }


    /**
     * 店铺商品上下架
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/setStoreGoodsUpState")
    public Response setStoreGoodsUpState(HttpServletRequest request) {
        String sgIds = request.getParameter("sgId");
//        String goodIds=request.getParameter("goodId");
//        String specIds=request.getParameter("specId");
        int sgId = 0;
        String upstate = "1";
        upstate = request.getParameter("upState");
        if (sgIds == null || sgIds == "") {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }

        if (StringUtil.isNumeric(sgIds)) {
            sgId = Integer.parseInt(sgIds);
        }

        StoreGoods storeGoods = new StoreGoods();
        storeGoods.setUpState(upstate);
        storeGoods.setSgId(sgId);
        int row = storeGoodsService.updateStoreGoodsUpState(storeGoods);
        if (row > 0) {
            return new Response(MessageConstants.MSG_SUCCESS, row, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
        }
    }

    /**
     * 更新店铺关注数量
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/updateConcern")
    public Response updateConcern(HttpServletRequest request) {
        String storeIds = request.getParameter("storeId");
        int storeId = 0;
        int concernCount = 0;
        if (storeIds == null || storeIds == "") {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        if (StringUtil.isNumeric(storeIds)) {
            storeId = Integer.parseInt(storeIds);
        }

        //查询关注的数量
        Store store1 = storeService.selectByPrimaryKey(storeId);
        if (store1 != null) {
            concernCount = store1.getConcernCount();
        }


        Store store = new Store();
        store.setStoreId(storeId);
        store.setConcernCount(concernCount + 1);
        int row = storeService.updateByPrimaryKeySelective(store);
        if (row > 0) {
            return new Response(MessageConstants.MSG_SUCCESS, row, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);

        }
    }

    /**
     * 设置店铺红包比例
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/setStoreWelfare")
    public Response setStoreWelfare(HttpServletRequest request) {
        String storeIds = request.getHeader("storeId");
        String welfares = request.getHeader("welfare");

        if (storeIds == null || storeIds == "" || welfares == null || welfares == "") {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        int storeId = 0;
        double welfare = 0.00;
        if (StringUtil.isNumeric(storeIds)) {
            storeId = Integer.parseInt(storeIds);
        }
        if (StringUtil.isNumeric(welfares)) {
            welfare = Double.parseDouble(welfares);
        }
        Store store = new Store();
        store.setStoreId(storeId);
        store.setWelfare(welfare);
        int row = storeService.updateByPrimaryKeySelective(store);
        if (row > 0) {
            return new Response(MessageConstants.MSG_SUCCESS, row, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
        }

    }

    /**
     * 获取店铺信息
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/getStoreInfo")
    public Response getStoreInfo(HttpServletRequest request) {
        String userNoIds = request.getParameter("userNoId");
        if (userNoIds == null || !StringUtil.isNumeric(userNoIds)) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        int userNoId = 0;
        userNoId = Integer.parseInt(userNoIds);
        Store store = storeService.selectStoreInfo(userNoId);

        if (store == null) {
            return new Response(MessageConstants.MSG_REQUEST_STATE, null, 0, "", request);
        }
        return new Response(MessageConstants.MSG_SUCCESS, store, 0, "", request);
    }

    /**
     * 更新保证金
     *
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/updateDeposits")
    public Response updateDeposits(HttpServletRequest request) {
        String userNoIds = request.getParameter("userNoId");
        String deposits = request.getParameter("deposit");

        if (userNoIds == null || userNoIds == "" || !StringUtil.isNumeric(userNoIds) || deposits == null || deposits == "" || !StringUtil.isNumeric(deposits)) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        int userNoId = 0;
        double deposit = 0;
        userNoId = Integer.parseInt(userNoIds);
        deposit = Double.parseDouble(deposits);
        Store store = new Store();
        store.setDeposit(deposit);
        store.setUserNoId(userNoId);
        int row = storeService.updateDeposits(store);
        if (row > 0) {
            return new Response(MessageConstants.MSG_SUCCESS, row, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
        }
    }

}
